package com.kmproject.haifu.mvp.ui.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jess.arms.utils.ArmsUtils;
import com.kmproject.haifu.R;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.ui.activity.HouseRentingShowDuanZuListActivity;

import java.util.List;

public class PopuWindowSelectSxItemNewAdapter extends BaseQuickAdapter<HousePayWayBean, BaseViewHolder> {
    String mdefaultSelectType;//控制单选还是多选
    private int selectDefaultIndex=-1;
    ItemClickCallBack mitemClickCallBack;
    int selectSexIndex=-1;
    public PopuWindowSelectSxItemNewAdapter(String defaultType,@Nullable List<HousePayWayBean> data,ItemClickCallBack itemClickCallBack) {
        super(R.layout.selectroom_item, data);
        mdefaultSelectType=defaultType;
        mitemClickCallBack=itemClickCallBack;
    }


    public void setSelectDefaultIndex(int selectDefaultIndex) {
        this.selectDefaultIndex = selectDefaultIndex;
    }

    public void setMdefaultSelectType(String mdefaultSelectType) {
        this.mdefaultSelectType = mdefaultSelectType;
    }
    @Override
    protected void convert(BaseViewHolder helper, HousePayWayBean item) {
        if (mdefaultSelectType==null)
        {
            mdefaultSelectType=item.getCode();
        }
        if (!"rentDetailList".equals(mdefaultSelectType))
        {
            //不允许多选
            if (selectDefaultIndex==helper.getPosition())
            {
                item.setSelect(true);
            }else {
                item.setSelect(false);
            }
        }
         helper.itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if ("rentDetailList".equals(mdefaultSelectType))
                 {
                     item.setSelect(!item.isSelect());
                 }else {
                     selectDefaultIndex=helper.getPosition();
                     mitemClickCallBack.callback(selectDefaultIndex,item);
                 }
                 if (item.isSelect())
                 {
                     if (selectSexIndex!=-1&&(item.getName().equals("限女生")||item.getName().equals("限男生")))
                     {
                         getData().get(selectSexIndex).setSelect(false);
                     }
                     if ((item.getName().equals("限女生")||item.getName().equals("限男生")))
                     {
                         selectSexIndex=helper.getPosition();
                     }

                 }else {
                     if (item.getName().equals("限女生")||item.getName().equals("限男生"))
                     {
                         selectSexIndex=-1;
                     }
                 }
                 notifyDataSetChanged();
             }
         });
        helper.<TextView>getView(R.id.housepublic_item_item_tv).setText(item.getName());
        if(item.isSelect()&&"rentDetailList".equals(mdefaultSelectType))
        {
            helper.<TextView>getView(R.id.housepublic_item_item_tv).setTextColor(ArmsUtils.getColor(mContext, R.color.color_191919));
            helper.<TextView>getView(R.id.housepublic_item_item_tv).setBackgroundResource(R.drawable.button_yellow_10_bg);
        }else if (helper.getPosition()==selectDefaultIndex&&!"rentDetailList".equals(mdefaultSelectType))
        {
            helper.<TextView>getView(R.id.housepublic_item_item_tv).setTextColor(ArmsUtils.getColor(mContext, R.color.color_191919));
            helper.<TextView>getView(R.id.housepublic_item_item_tv).setBackgroundResource(R.drawable.button_yellow_10_bg);
        }else {
            helper.<TextView>getView(R.id.housepublic_item_item_tv).setTextColor(ArmsUtils.getColor(mContext, R.color.white));
            helper.<TextView>getView(R.id.housepublic_item_item_tv).setBackgroundResource(R.drawable.button_gray_10_bg);
        }
}
public interface ItemClickCallBack{
        public void callback(int position, HousePayWayBean housePayWayBean);
}
}
